home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Enigma Amiga CD / Listati / 63-Marzo-Listato3.c < prev    next >
C/C++ Source or Header  |  1992-09-02  |  6KB  |  178 lines

  1. /****************************************************************************
  2. * Listato 3                                                                 *
  3. * Apre due finestre su uno schermo hires con porta condivisa; la prima e'   *
  4. * provvista di gadget di chiusura ed e' spostabile, la seconda e' una fines-*
  5. * tra borderless; se viene selezionato il gadget di chiusura della prima fi-*
  6. * nestra, il programma viene terminato; se si attiva il pulsante del mouse  *
  7. * sulla finestra borderless questa viene chiusa.                            *
  8. * Questo programma inizia ad essere strutturato in maniera piu' ordinata;   *
  9. * osservate infatti che le operazioni di apertura vengono effettuate nella  *
  10. * procedura OpenAll(), mentre quelle di chiusura nella procedura CloseAll();*
  11. * in quest'ultima viene sempre verificato che una cosa sia stata aperta     *
  12. * prima di chiuderla, in questa maniera tale procedura e' chiamata dalla    *
  13. * OpenAll() in caso di fallimento senza alcun pericolo, riducendo il codice *
  14. * della OpenAll().                                                          *
  15. ****************************************************************************/
  16.  
  17. #define INTUI_V36_NAMES_ONLY
  18.  
  19. /* inclusione file di supporto */
  20. #include <exec/types.h>
  21. #include <intuition/intuition.h>
  22. #include <intuition/screens.h>
  23. #include <graphics/modeid.h>
  24. #include <clib/exec_protos.h>
  25. #include <clib/intuition_protos.h>
  26. #include <clib/dos_protos.h>
  27.  
  28. struct Library *IntuitionBase = NULL;     /* puntatore a intuition.library */
  29. struct Screen *schermo = NULL;            /* puntatore allo schermo */
  30. struct Window *finestra1 = NULL;          /* puntatore alla finestra 1 */
  31. struct Window *finestra2 = NULL;          /* puntatore alla finestra 2 */
  32. struct MsgPort *IDCMPPort = NULL;         /* puntatore alla porta IDCMP */
  33.  
  34. /* definizione prototipi di funzione */
  35. void StripIntuiMessage(struct MsgPort *, struct Window *);
  36. void CloseWindowSafely(struct Window *);
  37. void WaitEvent(struct MsgPort *,struct IntuiMessage *);
  38. void CloseAll(void);
  39. void OpenAll(void);
  40.  
  41. void StripIntuiMessage(struct MsgPort *mp, struct Window *win)
  42. {
  43.   struct IntuiMessage *msg;
  44.   struct Node *succ;
  45.  
  46.   msg = (struct IntuiMessage *)mp->mp_MsgList.lh_Head;
  47.  
  48.   if (msg != NULL)
  49.   {
  50.     while (succ = msg->ExecMessage.mn_Node.ln_Succ)
  51.     {
  52.       if (msg->IDCMPWindow == win)
  53.       {
  54.         Remove(msg);
  55.         ReplyMsg(msg);
  56.       }
  57.       msg = (struct IntuiMessage *)succ;
  58.     }
  59.   }
  60.   return;
  61. }
  62.  
  63. void CloseWindowSafely(struct Window *win)
  64. {
  65.   Forbid();
  66.   StripIntuiMessage(win->UserPort,win);
  67.   win->UserPort = NULL;
  68.   ModifyIDCMP(win,NULL);
  69.   Permit();
  70.   CloseWindow(win);
  71.   return;
  72. }
  73.  
  74. void WaitEvent(struct MsgPort *porta,struct IntuiMessage *mess)
  75. {
  76.   struct IntuiMessage *msg;
  77.  
  78.   while ((msg = (struct IntuiMessage *)GetMsg(porta)) == NULL)
  79.     WaitPort(porta);
  80.  
  81.   CopyMem(msg,mess,sizeof(struct IntuiMessage));
  82.   return;
  83. }
  84.  
  85. /* procedura CloseAll() */
  86. void CloseAll()
  87. {
  88.   if (finestra2 != NULL) CloseWindowSafely(finestra2);
  89.   if (finestra1 != NULL) CloseWindowSafely(finestra1);
  90.   if (schermo != NULL) CloseScreen(schermo);
  91.   if (IDCMPPort != NULL) DeleteMsgPort(IDCMPPort);
  92.   if (IntuitionBase != NULL) CloseLibrary(IntuitionBase);
  93.  
  94.   exit(0);
  95. }
  96.  
  97. /* procedura OpenAll() */
  98. void OpenAll()
  99. {
  100.   UWORD Penne[] = { ~0 };
  101.  
  102.   /* apriamo intuition.library, almeno version 36 */
  103.   if ((IntuitionBase = OpenLibrary("intuition.library",36L)) == NULL)
  104.     CloseAll();
  105.  
  106.   /* apriamo una porta messaggi */
  107.   if ((IDCMPPort = CreateMsgPort()) == NULL)
  108.     CloseAll();
  109.  
  110.   /* apriamo uno schermo con look 3D, hires, 2 bitplanes, e overscan OSCAN_TEXT */
  111.   if ((schermo = OpenScreenTags(NULL,SA_Pens,Penne,
  112.                                      SA_Depth,2,
  113.                                      SA_DisplayID,HIRES_KEY,
  114.                                      SA_Title,"Schermo esempio.",
  115.                                      SA_Type,CUSTOMSCREEN,
  116.                                      TAG_END)) == NULL)
  117.     CloseAll();
  118.  
  119.   /* apre la prima finestra spostabile, con il gadget di chiusura */
  120.   if ((finestra1 = OpenWindowTags(NULL,WA_Left,10,
  121.                                        WA_Top,10,
  122.                                        WA_Width,350,
  123.                                        WA_Height,150,
  124.                                        WA_Title,"Finestra principale.",
  125.                                        WA_DragBar,TRUE,
  126.                                        WA_CloseGadget,TRUE,
  127.                                        WA_CustomScreen,(ULONG)schermo,
  128.                                        WA_IDCMP,NULL,
  129.                                        TAG_END)) == NULL)
  130.     CloseAll();
  131.   finestra1 -> UserPort = IDCMPPort;
  132.   ModifyIDCMP(finestra1,IDCMP_CLOSEWINDOW);
  133.  
  134.   /* apre la seconda finestra senza bordi */
  135.   if ((finestra2 = OpenWindowTags(NULL,WA_Left,300,
  136.                                        WA_Top,100,
  137.                                        WA_Width,200,
  138.                                        WA_Height,100,
  139.                                        WA_Borderless,TRUE,  /* senza bordi */
  140.                                        WA_CustomScreen,(ULONG)schermo,
  141.                                        WA_IDCMP,NULL,
  142.                                        TAG_END)) == NULL)
  143.     CloseAll();
  144.   finestra2 -> UserPort = IDCMPPort;
  145.   ModifyIDCMP(finestra2,IDCMP_MOUSEBUTTONS);
  146.  
  147.   return;
  148. }
  149.  
  150. void main()
  151. {
  152.   struct IntuiMessage mm;
  153.  
  154.   OpenAll();  /* apre tutte le strutture dati */
  155.   while(1)
  156.   {
  157.     WaitEvent(IDCMPPort,&mm);  /* attende un evento */
  158.     if (mm.IDCMPWindow == finestra1)
  159.     {
  160.       if (mm.Class == IDCMP_CLOSEWINDOW) break;
  161.       /* se la finestra e' finestra1 e l'evento e' IDCMP_CLOSEWINDOW allora
  162.          interrompi il ciclo infinito ed esci */
  163.     }
  164.     else if (mm.IDCMPWindow == finestra2)
  165.     {
  166.       if (mm.Class == IDCMP_MOUSEBUTTONS)
  167.       {
  168.         CloseWindowSafely(finestra2);
  169.         finestra2 = NULL;
  170.         /* se la finestra e' finestra2 e l'evento e' IDCMP_MOUSEBUTTONS allora
  171.            chiudi la finestra e imposta finestra2 a NULL (in modo da non farla
  172.            richiudere in CloseAll()) */
  173.       }
  174.     }
  175.   }
  176.   CloseAll();  /* chiudi tutto ed esci */
  177. }
  178.